package com.winning.devops.batch.reader.ch05;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * @author chensj
 * @date 2020-03-02 11:32
 */
@Configuration
public class JsonItemDemoConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    @Qualifier("commonJsonItemWriter")
    private ItemWriter<? super Cat> commonJsonItemWriter;


    @Bean
    public Job jsonItemDemoJob() {
        return jobBuilderFactory.get("jsonItemDemoJob")
                .start(jsonItemDemoStep())
                .build();
    }

    @Bean
    public Step jsonItemDemoStep() {
        return stepBuilderFactory
                .get("jsonItemDemoStep")
                .<Cat, Cat>chunk(10)
                .reader(commonJsonItemReader())
                .writer(commonJsonItemWriter)
                .build();
    }


    @Bean
    @StepScope
    public CommonJsonItemReader<Cat> commonJsonItemReader() {
        ClassPathResource resource = new ClassPathResource("test.json");
        return new CommonJsonItemReader(resource, new JacksonJsonObjectReader<>(Cat.class));
    }

}
